package com.inmaytide.website.service.system.impl;

import com.google.common.collect.Sets;
import com.inmaytide.website.domain.entities.system.SysRole;
import com.inmaytide.website.mapper.system.SysRoleMapper;
import com.inmaytide.website.service.system.SysRoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * @author lomiao
 * @since May 14, 2016
 */
@Service
@Transactional(readOnly = true)
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper mapper;

    @Override
    public Set<String> findUserRoleCodes(Long uid) {
        Set<String> set = Sets.newHashSet();
        findUserRoles(uid).forEach(role -> set.add(role.getRoleCode()));
        return set;
    }

    @Override
    @Transactional
    public void insert(SysRole inst) {
        mapper.insert(inst);
    }

    @Override
    public List<SysRole> findUserRoles(Long uid) {
        return mapper.findByUser(uid);
    }

    @Override
    public SysRole get(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public SysRole findByCode(String code) {
        SysRole condition = new SysRole();
        condition.setRoleCode(code);
        List<SysRole> list = findList(condition);
        return CollectionUtils.isNotEmpty(list) ? list.get(0) : null;
    }

    @Override
    public List<SysRole> findList(SysRole condition) {
        return mapper.selectByExample(condition);
    }

    @Override
    @Transactional
    public void update(SysRole inst) {
        mapper.updateByPrimaryKey(inst);
    }

}
